package cn.iocoder.yudao.module.distm.dal.mysql.schedule;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.distm.controller.admin.schedule.vo.ScheduleRecordPageReqVO;
import cn.iocoder.yudao.module.distm.controller.app.schedulerecord.vo.AppDeliveryPageReqVO;
import cn.iocoder.yudao.module.distm.controller.app.schedulerecord.vo.AppScheduleRecordPageReqVO;
import cn.iocoder.yudao.module.distm.dal.dataobject.schedule.ScheduleRecordDO;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDate;
import java.time.LocalTime;

/**
 * 配送管理-调度生产记录 Mapper
 *
 * @author yisl
 */
@Mapper
public interface ScheduleRecordMapper extends BaseMapperX<ScheduleRecordDO> {

    default PageResult<ScheduleRecordDO> selectPage(ScheduleRecordPageReqVO reqVO) {
        LocalTime depTime = ObjectUtil.isNotEmpty(reqVO.getDepTime())?LocalTime.parse(reqVO.getDepTime()):null;
        LocalTime inboundTime = ObjectUtil.isNotEmpty(reqVO.getInboundTime())?LocalTime.parse(reqVO.getInboundTime()):null;
        LocalTime preLandingTime = ObjectUtil.isNotEmpty(reqVO.getPreLandingTime())?LocalTime.parse(reqVO.getPreLandingTime()):null;
        LocalDate recordDate = ObjectUtil.isNotEmpty(reqVO.getRecordDate())?LocalDate.parse(reqVO.getRecordDate()):null;

        return selectPage(reqVO, new LambdaQueryWrapperX<ScheduleRecordDO>()
                .eqIfPresent(ScheduleRecordDO::getFlightNo, reqVO.getFlightNo())
                .eqIfPresent(ScheduleRecordDO::getDepTime, depTime)
                .eqIfPresent(ScheduleRecordDO::getFlightCrew, reqVO.getFlightCrew())
                .eqIfPresent(ScheduleRecordDO::getInboundFlight, reqVO.getInboundFlight())
                .eqIfPresent(ScheduleRecordDO::getMachineNo, reqVO.getMachineNo())
                .eqIfPresent(ScheduleRecordDO::getInboundTime, inboundTime)
                .eqIfPresent(ScheduleRecordDO::getPreLandingTime, preLandingTime)
                .eqIfPresent(ScheduleRecordDO::getRecordDate, recordDate)
                .orderByAsc(ScheduleRecordDO::getRecordDate)
                .orderByAsc(ScheduleRecordDO::getDepTime));
    }

    default PageResult<ScheduleRecordDO> appSelectPage(AppScheduleRecordPageReqVO reqVO) {
        LambdaQueryWrapperX<ScheduleRecordDO> queryWrapperX = new LambdaQueryWrapperX<ScheduleRecordDO>()
                .likeIfPresent(ScheduleRecordDO::getFlightNo, reqVO.getFlightNo());
        switch (reqVO.getSort()) {
            case "1":
                queryWrapperX.orderByDesc(ScheduleRecordDO::getPreLandingTime);
                break;
            case "2":
                queryWrapperX.orderByAsc(ScheduleRecordDO::getPreLandingTime);
                break;
            case "3":
                queryWrapperX.orderByDesc(ScheduleRecordDO::getDepTime);
                break;
            case "4":
                queryWrapperX.orderByAsc(ScheduleRecordDO::getDepTime);
                break;
        }
        return selectPage(reqVO, queryWrapperX);
    }

    default PageResult<ScheduleRecordDO> appTodaySelectPage(AppDeliveryPageReqVO reqVO) {
        LambdaQueryWrapperX<ScheduleRecordDO> queryWrapperX = new LambdaQueryWrapperX<ScheduleRecordDO>()
                .likeIfPresent(ScheduleRecordDO::getFlightCrew, reqVO.getUserId());
        return selectPage(reqVO, queryWrapperX);
    }

}